<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Fifeiyongkemu extends Common
{

	//列表
    public function index()
    {
        $sn = $this->request->get('sn');
        $title = $this->request->get('title');
        $this->assign(array('sn'=>$sn,'title'=>$title));
        $where = [];
        if($sn)
            $where['sn'] = array('like','%'.$sn.'%');
        if($title)
            $where['title'] = array('like','%'.$title.'%');
        $count = Db::table('cg_fifeiyongkemu')->where($where)->count();
        $data = Db::table('cg_fifeiyongkemu')->order("id desc")->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('maxpage',$maxpage);
        $this->assign('count',$count);
        return $this->fetch('index');
    }
    public function add(){

        $validate = new Validate([
            'title'  => 'require|max:100|unique:cg_fifeiyongkemu',
            'sn' => 'require|max:15|unique:cg_fifeiyongkemu'
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['sn'] = $this->request->post('sn');
        $data['brief'] = $this->request->post('brief');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_fifeiyongkemu')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

    public function update(){

        $id = $this->request->post('id');
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['sn'] = $this->request->post('sn');
        $data['id'] = $id;
        $data['brief'] = $this->request->post('brief');
        $validate = new Validate([
            'title'  => 'require|max:100|unique:cg_fifeiyongkemu',
            'sn' => 'require|max:15|unique:cg_fifeiyongkemu',
            'id' => 'require|number'
        ]);

        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $sncnt = Db::table('cg_fifeiyongkemu')->where('id !=\''.$id.'\' and sn = \''.$data['sn'].'\'')->count();
        if($sncnt){
            echo  json_encode([
                "success"=>false,
                "msg"=>'科目编号已存在'
            ]);die;
        }

        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_fifeiyongkemu')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

    public function delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_fifeiyongkemu')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }
}
